package org.jeecgframework.web.cgform.engine;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateDirectiveModel;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.jeecgframework.core.online.util.FreemarkerHelper;
import org.jeecgframework.core.util.PropertiesUtil;
import org.jeecgframework.web.cgform.common.CgAutoListConstant;
import org.jeecgframework.web.cgform.service.config.CgFormFieldServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Locale;
import java.util.Map;

@Component("templetContext")
public class TempletContext {

    @Resource(name = "freemarker")
    private Configuration freemarker;

    @Autowired
    private CgFormFieldServiceI cgFormFieldService;

    private Map<String, TemplateDirectiveModel> tags;

    private static final String ENCODING = "UTF-8";

    private static Cache ehCache;//ehcache
    /**
     * 系统模式：
     * PUB-生产（使用ehcache）
     * DEV-开发
     */
    private static String sysMode1 = null;

    static {
        PropertiesUtil util = new PropertiesUtil("sysConfig.properties");
        sysMode1 = util.readProperty(CgAutoListConstant.SYS_MODE_KEY);
        if (CgAutoListConstant.SYS_MODE_PUB.equalsIgnoreCase(sysMode1)) {
            ehCache = CacheManager.getInstance().getCache("dictCache");//永久缓存块
        }
    }

    @PostConstruct
    public void init() {
        if (tags == null) {
            return;
        }
        for (String key : tags.keySet()) {
            freemarker.setSharedVariable(key, tags.get(key));
        }
    }

    public Locale getLocale() {
        return freemarker.getLocale();
    }

    public Template getTemplate(String tableName, String ftlVersion) {
        Template template = null;
        if (tableName == null) {
            return null;
        }
        String oldTableName = tableName;

        if (ftlVersion != null && ftlVersion.length() > 0) {
            tableName = tableName + "&ftlVersion=" + ftlVersion;
        }

        try {
            if (CgAutoListConstant.SYS_MODE_DEV.equalsIgnoreCase(sysMode1)) {//开发模式
                template = freemarker.getTemplate(tableName, freemarker.getLocale(), ENCODING);
            } else if (CgAutoListConstant.SYS_MODE_PUB.equalsIgnoreCase(sysMode1)) {//生产模式（缓存）
                //获取版本号
                String version = cgFormFieldService.getCgFormVersionByTableName(oldTableName);
                template = getTemplateFromCache(tableName, ENCODING, version);
            } else {
                throw new RuntimeException("sysConfig.properties的freeMarkerMode配置错误：(PUB:生产模式，DEV:开发模式)");
            }
            return template;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 从缓存中读取ftl模板
     *
     * @param tableName
     * @param encoding
     * @return
     */
    public Template getTemplateFromCache(String tableName, String encoding, String version) {
        Template template = null;
        try {
            //cache的键：类名.方法名.参数名
            String cacheKey = FreemarkerHelper.class.getName() + ".getTemplateFormCache." + tableName + "." + version;
            Element element = ehCache.get(cacheKey);
            if (element == null) {
                template = freemarker.getTemplate(tableName, freemarker.getLocale(), ENCODING);
                element = new Element(cacheKey, template);
                ehCache.put(element);
            } else {
                template = (Template) element.getObjectValue();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return template;
    }

    /**
     * 从缓存中读取ftl模板
     *
     * @param tableName
     * @return
     */
    public void removeTemplateFromCache(String tableName) {
        try {
            //获取版本号
            String version = cgFormFieldService.getCgFormVersionByTableName(tableName);
            //cache的键：类名.方法名.参数名
            String cacheKey = FreemarkerHelper.class.getName() + ".getTemplateFormCache." + tableName + "." + version;
            ehCache.remove(cacheKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Configuration getFreemarker() {
        return freemarker;
    }

    public void setFreemarker(Configuration freemarker) {
        this.freemarker = freemarker;
    }

    public Map<String, TemplateDirectiveModel> getTags() {
        return tags;
    }

    public void setTags(Map<String, TemplateDirectiveModel> tags) {
        this.tags = tags;
    }

    public void clearCache() {
        try {
            ehCache.removeAll();
        } catch (Exception e) {

        }
    }

}
